﻿

//

using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using LaserMark.Components.Services;
using LaserMark.Control.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace LaserMark.Components;
public partial class DeviceStatus
{
    private Color TransUpDown { get; set; } = Color.Secondary;

    private Color TransArrow { get; set; } = Color.Secondary;

    private Color TransReserve { get; set; } = Color.Secondary;

    private Color DesktopSwitch { get; set; } = Color.Secondary;

    private Color Light { get; set; } = Color.Secondary;

    private Color RequestPice { get; set; } = Color.Secondary;

    private Color AppRun { get; set; } = Color.Secondary;

    private Color AppPause { get; set; } = Color.Secondary;

    private Color Stop { get; set; } = Color.Secondary;

    private Color Drive { get; set; } = Color.Secondary;

    [Inject]
    [NotNull]
    private IGlobalStatus? GlobalStatus { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        System.Timers.Timer timer = new System.Timers.Timer(500);
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
    }

    private async void Timer_Elapsed(object? sender, ElapsedEventArgs e)
    {

        try
        {
            if (GlobalStatus.IsRunning)
            {
                mc.GT_GetDo(0, mc.MC_GPO, out int value);
                var status = Convert.ToString(value, 2);

                mc.GT_GetDi(0, mc.MC_GPI, out int value1);
                var status1 = Convert.ToString(value1, 2).PadLeft(16, '0');

                if (status[6] == '0')
                {
                    TransUpDown = Color.Danger;
                }

                if (status[6] == '1')
                {
                    TransUpDown = Color.Success;
                }

                if (status[8] == '0')
                {
                    TransArrow = Color.Danger;
                }

                if (status[8] == '1')
                {
                    TransArrow = Color.Success;
                }

                if (status[7] == '0')
                {
                    TransReserve = Color.Danger;
                }

                if (status[7] == '1')
                {
                    TransReserve = Color.Success;
                }

                if (status1[4] == '1')
                {
                    DesktopSwitch = Color.Danger;
                }

                if (status1[4] == '0')
                {
                    DesktopSwitch = Color.Success;
                }

                if (status1[8] == '1')
                {
                    Light = Color.Danger;
                }

                if (status1[8] == '0')
                {
                    Light = Color.Success;
                }

                if (status[4] == '0')
                {
                    RequestPice = Color.Danger;
                }

                if (status[4] == '1')
                {
                    RequestPice = Color.Success;
                }

                if (status1[15] == '0')
                {
                    AppRun = Color.Danger;
                }
                else
                {
                    AppRun = Color.Success;
                }

                if (status1[14] == '0')
                {
                    AppPause = Color.Danger;
                }
                else
                {
                    AppPause = Color.Success;
                }

                if (status1[13] == '1')
                {
                    Stop = Color.Danger;
                }
                else
                {
                    Stop = Color.Success;
                }

                if (status1[6] == '0')
                {
                    Drive = Color.Danger;
                }
                else
                {
                    Drive = Color.Success;
                }
                await InvokeAsync(StateHasChanged);
                await Task.Delay(500);
            }
        }
        catch (Exception)
        {

        }

    }
}
